﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebAppAuthAdmin.AuthApiManagementServiceReference;
using WebAppAuthAdmin.Code;
using Web.Framework;
using AuthApi.Contracts;
using WebAppAuthAdmin.Code.Security;
using AuthApi.Contracts.Search;

namespace WebAppAuthAdmin.App
{
    public partial class OperationViewRoles : BasePage
    {
        private OperationIncluded operationCurrent
        {
            get
            {
                return (OperationIncluded)ViewState["OperationIncluded"];
            }
            set
            {
                ViewState["OperationIncluded"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HasPermissionToSeeMe(AppSecurity.APP_ROLE_VIEW, AppSecurity.APP_OPERATION_VIEW);

                if (!this.IsPostBack)
                {                    
                    if (!String.IsNullOrEmpty(this.Request.QueryString["Id"]))
                    {
                        int operationId = Convert.ToInt32(this.Request.QueryString["Id"]);

                        SearchOperation search = new SearchOperation();
                        search.Id = operationId;
                        search.LoadRelatedRoles = true;

                        operationCurrent = WebServiceHelper.GetOperation(search);
                        Bind();
                    }
                    else
                    {
                        ExceptionManager.Instance.ShowWarning("No se proporcionó el paramtro aplicación o rol");
                    }
                }


            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void Bind()
        {
            this.txtAplicacion.Text = operationCurrent.ApplicationName;
            this.lblOperationName.Text = operationCurrent.Name;
            this.lblOperationDescription.Text = operationCurrent.Description;

            this.Grid.DataSource = operationCurrent.RoleIncluded;
            this.Grid.DataBind();            
        }

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {                
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}